<template>
  <j-modal :title="title" :width="800" :visible="visible" :maskClosable="false" @close="close" destroyOnClose>
    <div style="margin: 0 auto" :class="{ isCheck: isCheck }">
      <a-form-model ref="form" :model="model" :rules="rules" layout="inline" class="ant-advanced-search-form">
        <a-row :gutter="24">
          <a-col :span="12">
            <a-form-model-item label="项目名称" prop="name">
              <a-input v-model="model.name" readOnly @click.native="openSearchModel"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="疑点名称" prop="ydmc">
              <a-input v-model="model.ydmc" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="疑点状态" prop="ydzt">
              <j-dict-select-tag style="width: 185px" v-model="model.ydzt" dict-code="generate_type" />
            </a-form-model-item>
          </a-col>
          <!-- </a-row> -->
          <!-- <a-row :gutter="24"> -->
          <!--          <a-col :span="12">-->
          <!--            <a-form-item label="疑点来源">-->
          <!--              <j-dict-select-tag style="width: 185px" v-model="model.ydlyCode" dict-code="ydly" />-->
          <!--            </a-form-item>-->
          <!--          </a-col>-->
          <a-col :span="12">
            <a-form-item label="记录时间">
              <j-date style="width: 100%" v-model="model.tjrq"></j-date>
            </a-form-item>
          </a-col>
        </a-row>
        <a-row :gutter="24">
          <a-col :span="24">
            <a-form-model-item label="疑点编码">
              <span> {{ model.ydbm ? model.ydbm : '系统生成' }}</span>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="疑点描述">
              <a-textarea :auto-size="{ minRows: 2, maxRows: 5 }" allow-clear v-model="model.ydnr" style="width: 570px" />
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="法规制度">
              <a-select v-model="model.fgzd" mode="tags" style="width: 570px">
                <template v-for="(item, index) in regulation">
                  <a-select-option :value="item.title || ''" :label="item.title" :key="item.id">
                    {{ item.title }}
                  </a-select-option>
                </template>
              </a-select>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="疑点现场">
              <span style="color: #ef6767; border-bottom: 1px solid #ef6767">
                <j-ellipsis :value="model.routerData" :length="75" />
              </span>
            </a-form-item>
          </a-col>
        </a-row>
        <a-row :span="24">
          <a-form-model-item label="附件上传">
            <RuleUpload ref="RuleUpload" :fileid="model.fileId" @uploadFile="uploadFile"></RuleUpload>
          </a-form-model-item>
        </a-row>
        <a-form-model-item label="现场快照">
          <img-upload
            ref="imgUpload"
            :dataURL="dataURL"
            :saveFile="saveFile"
            @returnFile="
              (imgList) => {
                this.model.images = imgList
              }
            "
          ></img-upload>
        </a-form-model-item>
      </a-form-model>
    </div>
    <!-- table区域-end -->
    <ProjectModel ref="projectModal" @getSelect="getProId" />
  </j-modal>
</template>
<script>
import { httpAction, getAction } from '@/api/manage'
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import imgUpload from '@/views/sj/components/imgUpload'
import RuleUpload from '@/views/sj/components/RuleUpload'
import * as lodash from 'lodash'
import ProjectModel from './ProjectModel.vue'

export default {
  name: 'YD',
  mixins: [JeecgListMixin],
  components: { imgUpload, RuleUpload, ProjectModel },
  props: ['routerData'],
  data() {
    return {
      model: {
        fileId: '',
        name: '',
        proId: '',
        ydnr: '',
        fgzd: [],
      },
      fileList: [],
      title: '新增疑点',
      labelCol: { span: 4 },
      wrapperCol: { span: 18 },
      visible: false,
      isCheck: false,
      disableMixinCreated: true,
      saveFile: '/sys/common/uploadMultiBase64',
      regulation: [],
      rules: {
        ydmc: [{ required: true, message: '不能为空!' }],
        ydzt: [{ required: true, message: '不能为空!' }],
        sjsx: [{ required: true, message: '不能为空!' }],
        // fgzd: [{ required: true, message: '不能为空!' }],
        name: [{ required: true, message: '不能为空!' }],
      },
      url: {
        list: '/modelTeam/queryProjectList',
        regulation: '/sjzszhly/knowledgeRegulation/queryKnowledgeRegulation',
        add: '/sjxmzygl/sjWorkZtfaEveYidian/addYidian',
      },
      dataURL: '',
    }
  },
  beforeCreate(){
    console.log('子组件预创建了');
  },
  created() {
    this.getRegulation()
    console.log('子组件创建了');
  },
  mounted(){
     console.log('子组件挂载完了');
  },
  watch: {
    'model.fgzd'(val){
      console.log('val---->',val);
    }
  },
  methods: {
    // 获取当前时间
    getNowTime: function () {
      let dateTime
      let yy = new Date().getFullYear()
      let mm = new Date().getMonth() + 1
      let dd = new Date().getDate()
      dateTime = yy + '-' + mm + '-' + dd
      console.log(dateTime)
      return dateTime
    },
    show(record, dataURL, ydnr, time, gzzd) {
        this.model = this.$options.data().model
        gzzd&&(Array.isArray(gzzd) ? (this.model.fgzd = [...gzzd]) : (this.model.fgzd.push(gzzd)))
        this.model.tjrq = this.getNowTime()
        this.model.ydnr = `分析${record.accName || record.proName ? `【${record.accName || record.proName} 】` : ''}${
          time ? `${time}` : ''
        }数据时，发现可疑信息可能是 ${ydnr ? ydnr : 'XXXX'}`
        this.model.routerData = this.routerData + '?' + this.formattData(record)
        this.dataURL = dataURL
        this.visible = true
    },
    getProId(project) {
      this.model.proId = project.id
      this.model.name = project.name
    },
    formattData(data) {
      console.log('data----------->', data)
      let a = Object.entries(data)
      let b = []
      a.forEach((item) => {
        if (item[1]) {
          // console.log(item, 'close')
          b.push(item.join('='))
        }
      })
      console.log('b----------->', b)
      return b.join('&')
    },
    openSearchModel() {
      this.$refs.projectModal.show()
    },
    close(isSubmit) {
      if (isSubmit) {
        this.saveData()
      } else {
        this.visible = false
      }
    },
    getRegulation() {
      getAction(this.url.regulation).then((res) => {
        if (res.success && res.result.length > 0) {
          this.regulation = res.result
        } else {
          console.log(res)
        }
      })
    },
    saveData() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          this.$refs.RuleUpload.saveFile('sjyd')
        } else {
          return false
        }
      })
    },
    uploadFile(id) {
      this.model.fileId = id
      if (this.model.fgzd) {
        this.model.fgzd = this.model.fgzd.toString()
      } else {
        this.model.fgzd = ''
      }
      httpAction(this.url.add, this.model, 'post').then((res) => {
        if (res.success) {
          this.$message.success(res.message)
          this.$emit('ok')
          this.visible = false
        } else {
          this.$message.error(res.message)
        }
      })
    },
  },
}
</script>
<style lang="css" scoped>
/deep/ .ant-upload-picture-card-wrapper {
  width: 660px !important;
}
</style>
